package com.taichuan.selfcheck.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.taichuan.selfcheck.R;
import com.taichuan.selfcheck.config.PropertyDefaultValue;
import com.taichuan.selfcheck.eventbus.FillLightPwmEvent;
import com.taichuan.selfcheck.eventbus.LightPowerEvent;
import com.taichuan.selfcheck.hardware.LightControlUtils;
import com.taichuan.selfcheck.mvp.presenter.LightControlPresenter;
import com.taichuan.selfcheck.mvp.viewinterface.LightControlInterface;
import com.taichuan.selfcheck.ui.dialog.ResultDialog;
import com.taichuan.selfcheck.ui.fragment.base.AppBaseFragment;
import com.taichuan.selfcheck.utils.HardwareUtils;
import com.taichuan.tcutils.taichuan.SystemUtils;
import com.taichuan.tcutils.taichuan.TcDeviceType;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 该类主要用于
 *
 * @author CamelLuo
 * @version 2019/12/5
 */
public class LightTestFragment extends AppBaseFragment<LightControlInterface, LightControlPresenter> implements LightControlInterface {
    private static final String TAG = LightTestFragment.class.getSimpleName();

    @BindView(R.id.fragment_light_test_fill_light_button)
    Button fillLightBtn;
    @BindView(R.id.light_module_value_progressbar)
    ProgressBar lightModuleValuePb;
    @BindView(R.id.light_module_value_textView)
    TextView lightModuleValueTv;
    @BindView(R.id.fragment_light_test_infrard_light_button)
    Button infrardLightBtn;
    @BindView(R.id.activity_test_device_light_module_layout)
    LinearLayout lightModuleValueLayout;

    ResultDialog resultDialog;
    @BindView(R.id.fragment_light_test_propmpt_light_button)
    Button propmptLightBtn;

    @BindView(R.id.fragment_light_test_color_light_button_1)
    Button colorLightBtn1;
    @BindView(R.id.fragment_light_test_color_light_button_2)
    Button colorLightBtn2;

    @BindView(R.id.light_module_filllight_pwm_layout)
    ViewGroup filllightPwmLayout;
    @BindView(R.id.light_module_filllight_pwm_cb)
    CheckBox fillLightPWMCb;
    @BindView(R.id.light_module_filllight_pwm_sb)
    SeekBar fillLightPWMSb;
    @BindView(R.id.light_module_filllight_pwm_tv)
    TextView fillLightPWMTv;

    @Override
    protected void getBundleValues(Bundle bundle) {

    }

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_light_test;
    }

    @Override
    protected void initView(View view, Bundle bundle) {
        EventBus.getDefault().register(this);

        fillLightBtn.setTag("close");
        infrardLightBtn.setTag("close");
        propmptLightBtn.setTag("close");
        colorLightBtn1.setTag("close");
        colorLightBtn2.setTag("close");

        lightModuleValuePb.setMax(4096);

        resultDialog = new ResultDialog(PropertyDefaultValue.FUNCTION_TEST_8, getResources().getString(R.string.light_test), getContext());            //设置弹窗发送的事件——灯光测试

        if (SystemUtils.isRk3399()) {
            infrardLightBtn.setVisibility(View.VISIBLE);
            if (!SystemUtils.isVankeSiteBoxDevice()){           //RK3399设备
                lightModuleValueLayout.setVisibility(View.VISIBLE);
            }else {                             //万科智慧工地项目设备
                lightModuleValueLayout.setVisibility(View.GONE);
            }

            if (SystemUtils.getTcDeviceType() == TcDeviceType.RK3399_VERTICAL_TOUCH_O8A_DOOR) {  //Tc-Y9D-O8A设备拥有提示灯
                propmptLightBtn.setVisibility(View.VISIBLE);
                infrardLightBtn.setVisibility(View.GONE);
            } else {
                propmptLightBtn.setVisibility(View.GONE);
            }
        } else {
            if (SystemUtils.getTcDeviceType() == TcDeviceType.A64_VERTICAL_TOUCH_HZ01){ //A64 TC-Y9D-HZ01 设备拥有提示灯和光感
                lightModuleValueLayout.setVisibility(View.VISIBLE);
                propmptLightBtn.setVisibility(View.VISIBLE);
            }else {
                lightModuleValueLayout.setVisibility(View.GONE);
                propmptLightBtn.setVisibility(View.GONE);
            }
            infrardLightBtn.setVisibility(View.GONE);
        }

        if (SystemUtils.isRk3399() && HardwareUtils.is20Devices()) {
            colorLightBtn1.setVisibility(View.VISIBLE);
            colorLightBtn2.setVisibility(View.VISIBLE);
            filllightPwmLayout.setVisibility(View.VISIBLE);
            // 补光灯
            fillLightPWMSb.setEnabled(false);
            fillLightPWMCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // 设置是否允许调光
                    fillLightPWMSb.setEnabled(isChecked);
                    // 首先关闭补光灯
                    LightControlUtils.getInstance().controlFillLight(false, false);
                    // 然后再调节亮度
                    LightControlUtils.getInstance().controlFillLightPwm(isChecked, fillLightPWMSb.getProgress());
                }
            });
            fillLightPWMSb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                    // PWM的占空比
                    fillLightPWMTv.setText(String.valueOf(progress));
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // 首先关闭补光灯
                    LightControlUtils.getInstance().controlFillLight(false, false);
                    // 然后再调节亮度
                    LightControlUtils.getInstance().controlFillLightPwm(fillLightPWMCb.isChecked(), fillLightPWMSb.getProgress());
                }
            });
        } else {
            colorLightBtn1.setVisibility(View.GONE);
            colorLightBtn2.setVisibility(View.GONE);
            filllightPwmLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);

        //关闭补光灯和红外灯
        LightControlUtils.getInstance().controlFillLight(false, false);
        if (SystemUtils.isRk3399()) {
            LightControlUtils.getInstance().openOrCloseInfraredLight(false);
        }
    }

    @Override
    protected LightControlPresenter createPresenter() {
        return new LightControlPresenter();
    }

    @OnClick({
            R.id.fragment_light_test_fill_light_button,
            R.id.fragment_light_test_infrard_light_button,
            R.id.fragment_light_test_propmpt_light_button,
            R.id.fragment_light_test_color_light_button_1,
            R.id.fragment_light_test_color_light_button_2
    })
    @Override
    protected void onXmlClick(View v) {
        super.onXmlClick(v);
        switch (v.getId()) {
            case R.id.fragment_light_test_fill_light_button: {
                if (fillLightBtn.getTag().equals("close")) {
                    //开启补光灯
                    LightControlUtils.getInstance().controlFillLight(true, false);
                    fillLightBtn.setTag("open");
                    fillLightBtn.setText(getStrById(R.string.close_fill_light));
                } else {
                    //关闭补光灯
                    LightControlUtils.getInstance().controlFillLight(false, false);
                    fillLightBtn.setTag("close");
                    fillLightBtn.setText(getStrById(R.string.open_fill_light));
                }
                break;
            }
            case R.id.fragment_light_test_infrard_light_button: {
                if (SystemUtils.isRk3399()) {
                    if (infrardLightBtn.getTag().equals("close")) {
                        //开启红外灯
                        LightControlUtils.getInstance().openOrCloseInfraredLight(true);
                        infrardLightBtn.setTag("open");
                        infrardLightBtn.setText(getStrById(R.string.close_infrared_light));
                    } else {
                        //关闭红外灯
                        LightControlUtils.getInstance().openOrCloseInfraredLight(false);
                        infrardLightBtn.setTag("close");
                        infrardLightBtn.setText(getStrById(R.string.open_infrared_light));
                    }
                } else {
                    showMsg("当前设备不支持控制红外灯");
                }
                break;
            }
            case R.id.fragment_light_test_propmpt_light_button: {
                if (SystemUtils.getTcDeviceType() == TcDeviceType.RK3399_VERTICAL_TOUCH_O8A_DOOR ||  //Tc-Y9D-O8A设备拥有提示灯
                SystemUtils.getTcDeviceType() == TcDeviceType.A64_VERTICAL_TOUCH_HZ01){             //A64 TC-Y9D-HZ01 设备拥有提示灯
                    if (propmptLightBtn.getTag().equals("close")) {
                        //开启红外灯
                        LightControlUtils.getInstance().openOrClosePromptLight(true);
                        propmptLightBtn.setTag("open");
                        propmptLightBtn.setText(getStrById(R.string.close_prompt_light));
                    } else {
                        //关闭红外灯
                        LightControlUtils.getInstance().openOrClosePromptLight(false);
                        propmptLightBtn.setTag("close");
                        propmptLightBtn.setText(getStrById(R.string.open_prompt_light));
                    }
                } else {
                    showMsg("当前设备不支持控制红外灯");
                }
                break;
            }
            case R.id.fragment_light_test_color_light_button_1: {
                if ("close".equals(colorLightBtn1.getTag())) {
                    colorLightBtn1.setTag("open");
                    colorLightBtn1.setText(R.string.close_color_light_1);
                    colorLightBtn2.setTag("close");
                    colorLightBtn2.setText(R.string.open_color_light_2);
                    LightControlUtils.getInstance().controlColorLight(0);
                } else {
                    colorLightBtn1.setTag("close");
                    colorLightBtn1.setText(R.string.open_color_light_1);
                    LightControlUtils.getInstance().controlColorLight(-1);
                }
                break;
            }
            case R.id.fragment_light_test_color_light_button_2: {
                if ("close".equals(colorLightBtn2.getTag())) {
                    colorLightBtn2.setTag("open");
                    colorLightBtn2.setText(R.string.close_color_light_2);
                    colorLightBtn1.setTag("close");
                    colorLightBtn1.setText(R.string.open_color_light_1);
                    LightControlUtils.getInstance().controlColorLight(1);
                } else {
                    colorLightBtn2.setTag("close");
                    colorLightBtn2.setText(R.string.open_color_light_2);
                    LightControlUtils.getInstance().controlColorLight(-1);
                }
                break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLightEvent(LightPowerEvent lightPowerEvent) {
        if (SystemUtils.isRk3399() ||
            SystemUtils.getTcDeviceType() == TcDeviceType.A64_VERTICAL_TOUCH_HZ01) {
            if (lightModuleValuePb != null && lightModuleValueTv != null) {
                Logger.t(TAG).e("获取到光感数值：  " + lightPowerEvent.getLight());
                lightModuleValuePb.setProgress(lightPowerEvent.getLight());
                lightModuleValueTv.setText(lightPowerEvent.getLight() + "");
            }
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFillLightPwmEvent(FillLightPwmEvent event) {
        Log.d(TAG, "handleMessage: SERIAL_FILL_LIGHT_PWM open=" + event.open + ", pwm=" + event.pwm);
        boolean isLightOpened = "open".equals(fillLightBtn.getTag());
        if (isLightOpened) {
            LightControlUtils.getInstance().controlFillLight(true, false);
        }
    }

    @Override
    protected boolean isAllowAutoFinishFragment() {
        return false;
    }

    @Override
    public void doOnAutoFinishFragment() {
        resultDialog.show();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (resultDialog.isShowing()) {
            resultDialog.dispatchKeyEvent(event);
        }
        if (event.getKeyCode() == KeyEvent.KEYCODE_POUND) {
            fillLightBtn.performClick();
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_1) {
            infrardLightBtn.performClick();
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_2) {
            propmptLightBtn.performClick();
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_3) {
            colorLightBtn1.performClick();
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_4) {
            colorLightBtn2.performClick();
        }
        return super.dispatchKeyEvent(event);
    }


}
